/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.oea.struts.actions;

import com.oea.hibernate.entities.Admin;
import com.oea.hibernate.entities.Faculty;
import com.oea.hibernate.entities.Student;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author unnati
 */
public class ForgotPasswordAction extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            String email = request.getParameter("email");
            String forgotted_password = "";
            
            if (request.getParameter("usertype").equals("admin"))
            {
                Admin a = new Admin();
                a = (Admin)a.getByEmail(email);
                forgotted_password = a.getPassword();
                System.out.println(forgotted_password);
                System.out.println("mmmmm");
            }
            else if (request.getParameter("usertype").equals("faculty"))
            {
                Faculty f = new Faculty();
                f = (Faculty)f.getByEmail(email);
                forgotted_password = f.getPassword();
            }
            else
            {
                Student stud = new Student();
                stud = (Student)stud.getByEmail(email);
                forgotted_password = stud.getPassword();
            }
            
           final String from="youremail";
                String to=email;
                String host="localhost";
                final String username = "root";
                final String pass = "password";

                Properties props = new Properties();
                props.put("mail.smtp.auth", "true");
                props.put("mail.smtp.starttls.enable", "true");
                props.put("mail.smtp.host", "smtp.gmail.com");
                props.put("mail.smtp.port", "587");

                Session s=Session.getInstance(props,  
                        new javax.mail.Authenticator() 
                        {
                            protected PasswordAuthentication getPasswordAuthentication() 
                            {
                                return new PasswordAuthentication(from, pass);
                            }
                        });
                try 
                {
                
                MimeMessage mm=new MimeMessage(s);
                mm.setFrom(new InternetAddress(from));
                mm.addRecipient(Message.RecipientType.TO,
                                  new InternetAddress(to));
                mm.setSubject("ToDay's Task_Deatails");
                mm.setText("Your Password is" + forgotted_password);
                Transport.send(mm);
                    
            } 
                catch (Exception e)
            {
                e.printStackTrace();
            }
               

    }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
